//
//  FCChangePinVC.m
//  FundChain
//
//  Created by 肖浩然的mac on 2018/3/5.
//  Copyright © 2018年 RUIXIN. All rights reserved.
//

#import "FCChangePinVC.h"

@interface FCChangePinVC ()<UITextFieldDelegate>{
    UIView     *_viewNav;
}

/* 旧pin */
@property (strong, nonatomic) UITextField *oldPinField;
/* 新pin */
@property (strong, nonatomic) UITextField *newpinTextField;
/* 确定pin */
@property (strong, nonatomic) UITextField *confirmPinField;
/* 完成按钮 */
@property (strong, nonatomic) UIButton *completeBtn;

//3个提示Label
/* <#Annotation#> */
@property (strong, nonatomic) UILabel *tipsLabel1;
@property (strong, nonatomic) UILabel *tipsLabel2;
@property (strong, nonatomic) UILabel *tipsLabel3;

@end

@implementation FCChangePinVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    [FCLocalUserHelper saveIsMainVC:@"0"];
    [self setNav];
    [self setupUI];
}
#pragma mark-setupUI
-(void)setupUI{
    //old pin
    UILabel *oldPinLabel = [UILabel new];
    [self.view addSubview:oldPinLabel];
    [oldPinLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_viewNav.mas_bottom).mas_offset(60*KScreenHeightScale1334);
        make.left.mas_equalTo(30*KScreenWidthScale750);
    }];
    oldPinLabel.textColor = kGrayColor;
    oldPinLabel.text = Localized(@"原来的pin");
    oldPinLabel.font = Kfont(32);
    //textfield
    UITextField *oldPinField = [UITextField new];
    self.oldPinField = oldPinField;
    [self.view addSubview:oldPinField];
    [oldPinField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(oldPinLabel);
        make.top.mas_equalTo(oldPinLabel.mas_bottom).offset(40*KScreenHeightScale1334);
        make.right.mas_equalTo(-30*KScreenWidthScale750);
    }];
    oldPinField.font = Kfont(36);
    oldPinField.secureTextEntry = YES;
    oldPinField.keyboardType = UIKeyboardTypeNumberPad;
    oldPinField.delegate = self;
     [oldPinField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    //分割线
    UIView *viewLine =[UIView new];
    [self.view addSubview:viewLine];
    [viewLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(oldPinField);
        make.top.mas_equalTo(oldPinField.mas_bottom);
        make.height.mas_equalTo(1.0);
    }];
    viewLine.backgroundColor =kLineColor;
    //提示label1
    UILabel *tipsLabel = [UILabel new];
    self.tipsLabel1 = tipsLabel;
    [self.view addSubview:tipsLabel];
    [tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewLine.mas_bottom).mas_offset(5*KScreenHeightScale1334);
        make.left.mas_equalTo(viewLine);
    }];
    tipsLabel.text = Localized(@"输入当前的pin");
    tipsLabel.textColor = kwarningColor;
    tipsLabel.font = Kfont(24);
    tipsLabel.hidden = YES;
    //newPin
    UILabel *newPinLabel = [UILabel new];
    [self.view addSubview:newPinLabel];
    [newPinLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewLine.mas_bottom).mas_offset(80*KScreenHeightScale1334);
        make.left.mas_equalTo(30*KScreenWidthScale750);
    }];
    newPinLabel.textColor = kGrayColor;
    newPinLabel.text = Localized(@"新的pin");
    newPinLabel.font = Kfont(32);
    //textfield
    UITextField *newPinField = [UITextField new];
    self.newpinTextField = newPinField;
    [self.view addSubview:newPinField];
    [newPinField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(newPinLabel);
        make.top.mas_equalTo(newPinLabel.mas_bottom).offset(40*KScreenHeightScale1334);
        make.right.mas_equalTo(-30*KScreenWidthScale750);
    }];
    newPinField.font = Kfont(36);
    newPinField.secureTextEntry = YES;
    newPinField.keyboardType = UIKeyboardTypeNumberPad;
    newPinField.delegate =self;
     [newPinField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    //分割线
    UIView *viewLine1 =[UIView new];
    [self.view addSubview:viewLine1];
    [viewLine1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(newPinField);
        make.top.mas_equalTo(newPinField.mas_bottom);
        make.height.mas_equalTo(1.0);
    }];
    viewLine1.backgroundColor =kLineColor;
    //提示label2
    UILabel *tipsLabel2 = [UILabel new];
    self.tipsLabel2 = tipsLabel2;
    [self.view addSubview:tipsLabel2];
    [tipsLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewLine1.mas_bottom).mas_offset(5*KScreenHeightScale1334);
        make.left.mas_equalTo(viewLine1);
    }];
    tipsLabel2.text = Localized(@"输入新的pin");
    tipsLabel2.textColor = kwarningColor;
    tipsLabel2.font = Kfont(24);
    tipsLabel2.hidden = YES;
    //confirmNewPin
    UILabel *confirmNewPinLabel = [UILabel new];
    [self.view addSubview:confirmNewPinLabel];
    [confirmNewPinLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewLine1.mas_bottom).mas_offset(80*KScreenHeightScale1334);
        make.left.mas_equalTo(30*KScreenWidthScale750);
    }];
    confirmNewPinLabel.textColor = kGrayColor;
    confirmNewPinLabel.text = Localized(@"确认新的pin");
    confirmNewPinLabel.font = Kfont(32);
    //textfield
    UITextField *confirmNewPinField = [UITextField new];
    self.confirmPinField = confirmNewPinField;
    [self.view addSubview:confirmNewPinField];
    [confirmNewPinField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(confirmNewPinLabel);
        make.top.mas_equalTo(confirmNewPinLabel.mas_bottom).offset(40*KScreenHeightScale1334);
        make.right.mas_equalTo(-30*KScreenWidthScale750);
    }];
    confirmNewPinField.font = Kfont(36);
    confirmNewPinField.secureTextEntry = YES;
    confirmNewPinField.delegate = self;
    confirmNewPinField.keyboardType = UIKeyboardTypeNumberPad;
    [confirmNewPinField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
     [confirmNewPinField addTarget:self action:@selector(textFieldDidEndChange:) forControlEvents:UIControlEventEditingDidEnd];
    //分割线
    UIView *viewLine2 =[UIView new];
    [self.view addSubview:viewLine2];
    [viewLine2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(confirmNewPinField);
        make.top.mas_equalTo(confirmNewPinField.mas_bottom);
        make.height.mas_equalTo(1.0);
    }];
    viewLine2.backgroundColor =kLineColor;
    //提示label3
    UILabel *tipsLabel3 = [UILabel new];
    self.tipsLabel3 = tipsLabel3;
    [self.view addSubview:tipsLabel3];
    [tipsLabel3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewLine2.mas_bottom).mas_offset(5*KScreenHeightScale1334);
        make.left.mas_equalTo(viewLine2);
    }];
    tipsLabel3.text = Localized(@"确认PIN不能为空");
    tipsLabel3.textColor = kwarningColor;
    tipsLabel3.font = Kfont(24);
    tipsLabel3.hidden = YES;
    //complete
    UIButton *completeBtn = [UIButton new];
    self.completeBtn = completeBtn;
    [self.view addSubview:completeBtn];
    [completeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30*KScreenWidthScale750);
        make.right.mas_equalTo(-30*KScreenWidthScale750);
        make.bottom.mas_equalTo(-100*KScreenHeightScale1334);
        make.height.mas_equalTo(110*KScreenHeightScale1334);
    }];
    completeBtn.layer.cornerRadius = 5.0;
    completeBtn.layer.masksToBounds = YES;
    completeBtn.titleLabel.font = KOverstrikingfont(36);
    [completeBtn setTitle:Localized(@"完成") forState:(UIControlStateNormal)];
    [completeBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
    [completeBtn setBackgroundColor:kLightblueColor];
    
    [completeBtn addTarget:self action:@selector(completeBtnClick) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark-设置输入框的限制
-(void)textFieldDidChange:(UITextField *)textField{
    
    if (self.oldPinField == textField) {
        if (textField.text.trimWhitespace.length > 0) {
            self.tipsLabel1.hidden = YES;
            
        }else{
            self.tipsLabel1.hidden = NO;
        }
        
    }
    if (self.newpinTextField == textField) {
        if (textField.text.trimWhitespace.length > 0) {
            self.tipsLabel2.hidden = YES;
            
        }else{
            self.tipsLabel2.hidden = NO;
        }
        
    }
    if (self.confirmPinField == textField) {
        if (textField.text.trimWhitespace.length > 0) {
            self.tipsLabel3.hidden = YES;
            
        }else{
            self.tipsLabel3.hidden = NO;
        }
        
    }
    
    if (self.newpinTextField == textField || self.confirmPinField == textField || self.oldPinField == textField) {
        
        if (textField.text.trimWhitespace.length > 6) {
            textField.text = [textField.text substringToIndex:6];
            
        }
        
        
    }
        
}
-(void)textFieldDidEndChange:(UITextField *)textField{
    if (self.confirmPinField == textField) {
        
        if (self.confirmPinField.text.trimWhitespace.length > 0 && self.newpinTextField.text.trimWhitespace.length > 0 && self.oldPinField.text.trimWhitespace.length > 0) {
            
            [self.completeBtn setBackgroundColor:kRedOrangeColor];
            
        }else{
            [self.completeBtn setBackgroundColor:kLightblueColor];
        }
        
        
    }
    
}
-(void)completeBtnClick{
    
    if (self.oldPinField.text.trimWhitespace.length <= 0) {
        self.tipsLabel1.hidden = NO;
        return;
    }
    if (self.newpinTextField.text.trimWhitespace.length <= 0) {
        self.tipsLabel2.hidden = NO;
        return;
    }
    if (self.confirmPinField.text.trimWhitespace.length <= 0 ) {
        
        self.tipsLabel3.hidden = NO;
        return;
        
    }

    if (![self.newpinTextField.text.trimWhitespace isEqualToString: self.confirmPinField.text.trimWhitespace]) {
        self.tipsLabel3.text = Localized(@"两次输入的PIN不一致");
        self.tipsLabel3.hidden = NO;
        return;
    }
    if ([self.oldPinField.text.trimWhitespace isEqualToString:self.newpinTextField.text.trimWhitespace]&&[self.oldPinField.text.trimWhitespace isEqualToString:self.confirmPinField.text.trimWhitespace]) {
        
        self.tipsLabel2.text = Localized(@"新Pin不能和旧Pin一致");
        self.tipsLabel2.hidden = NO;
        return;
    }
    [self showChrysanthemumAlert:nil];
    //先验证Pin
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
    NSArray *arrM =[FCLocalUserHelper getTimeAndSign];
    NSString *signStr = [NSString stringWithFormat:@"%@",arrM[1]];
    NSString *timeStr = [NSString stringWithFormat:@"%@",arrM[0]];
    [dict setObject:[FCLocalUserHelper sha1:signStr] forKey:@"sign"];
    [dict setObject:timeStr forKey:@"time"];
    NSString *xidStr = [FCLocalUserHelper getXidStr];
    [dict setObject:xidStr forKey:@"XID"];
    [dict setObject:[FCLocalUserHelper sha1:self.oldPinField.text.trimWhitespace] forKey:@"PIN"];
    NSString *url = Pin_verification_URL;
    
    [FCSharedNetworkTool POSTWithURLString:url parameters:dict success:^(id responseObject) {
        if (responseObject) {
            if ([responseObject[@"code"]integerValue] == 200) {
                [self hideProgress];
                NSString *pinTokenStr = responseObject[@"result"][@"pinToken"];
                
                [self changePin:pinTokenStr];
            }else if ([responseObject[@"code"]integerValue] == 409){
                [self hideProgress];
                [self showAlert:Localized(@"PIN错误次数太多，请重设PIN后重试!")];
            }else if ([responseObject[@"code"]integerValue] == 406){
                [self hideProgress];
                self.tipsLabel1.text = Localized(@"Pin不正确，请重试!");
                self.tipsLabel1.hidden = NO;
            }
        }
        
    } faile:^(NSError *error) {
        if (error) {
            NSLog(@"%@",error);
        }
        
    }];
    
}
-(void)changePin:(NSString*)pinTokenStr{
    
    [self showChrysanthemumAlert:nil];
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
    [dict setObject:[FCLocalUserHelper sha1:self.newpinTextField.text.trimWhitespace] forKey:@"newPIN"];
    NSArray *arrM =[FCLocalUserHelper getTimeAndSign];
    NSString *signStr = [NSString stringWithFormat:@"%@",arrM[1]];
    NSString *timeStr = [NSString stringWithFormat:@"%@",arrM[0]];
    [dict setObject:[FCLocalUserHelper sha1:signStr] forKey:@"sign"];
    [dict setObject:timeStr forKey:@"time"];
    NSString *xidStr = [FCLocalUserHelper getXidStr];
    [dict setObject:xidStr forKey:@"XID"];
    [dict setObject:pinTokenStr forKey:@"pinToken"];
    NSString *url = Chance_pin_URL;
    
    [FCSharedNetworkTool POSTWithURLString:url parameters:dict success:^(id responseObject) {
        if (responseObject) {
            if ([responseObject[@"code"]integerValue] == 200) {
                [self hideProgress];
//                [self showAlert:Localized(@"成功")];
                [self.navigationController popViewControllerAnimated:YES];
            }else if ([responseObject[@"code"]integerValue] == 400){
                [self hideProgress];
                [self showAlert:Localized(@"失败")];
            }
        }
        
    } faile:^(NSError *error) {
        if (error) {
            NSLog(@"%@",error);
        }
        
    }];
    
}
#pragma mark-设置导航栏
-(void)setNav{
    self.navigationController.navigationBarHidden = YES;
    _viewNav =[UIView new];
    [self.view addSubview:_viewNav];
    [_viewNav mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            if (iPhoneX) {
                
                make.left.right.top.mas_equalTo(self.view.safeAreaInsets);
                make.height.mas_equalTo(88);
            }else{
                make.left.right.mas_equalTo(0);
                make.height.mas_equalTo(64);
                make.top.mas_equalTo(0);
            }
        }else{
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(64);
            make.top.mas_equalTo(0);
        }
        
    }];
    _viewNav.backgroundColor = kRedOrangeColor;
    
    UIButton *leftBtn = [UIButton new];
    [self.view addSubview:leftBtn];
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15*KScreenWidthScale750);
        make.centerY.mas_equalTo(_viewNav.mas_centerY).offset(8);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
     [leftBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
    [leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    //返回
    UILabel *backLabel = [UILabel new];
    
    [_viewNav addSubview:backLabel];
    [backLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_viewNav);
        make.centerY.mas_equalTo(_viewNav.mas_centerY).offset(8);
    }];
    backLabel.text = Localized(@"更改PIN码");
    backLabel.textColor = [UIColor whiteColor];
    backLabel.font = KOverstrikingfont(36);
    
    //事件
    [leftBtn addTarget:self action:@selector(leftBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)leftBtnClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //移除第一响应
    [self.oldPinField resignFirstResponder];
    [self.newpinTextField resignFirstResponder];
    [self.confirmPinField resignFirstResponder];
}
@end

